/*	$Id: stringtest.c 983 2009-09-01 02:45:42Z phrakt $	*/
/*
 * Copyright (c) 2008 Jean-Francois Brousseau <phrakt@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/types.h>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <canopy/log.h>
#include <canopy/string.h>

#include "test.h"




int
test_strdup(void)
{
	int ret;
	char orig[] = "test_string";
	char *copy;

	copy = cnp_strdup(orig);
	if (copy == NULL) {
		return (TEST_FAIL);
	}

	if (copy == orig)
		return (TEST_FAIL);

	if (strcmp(copy, orig) != 0) {
		return (TEST_FAIL);
	}

	return (TEST_OK);
}

int
test_strndup(void)
{
	int ret;
	char orig[] = "af56odisaufsiadjg";
	char *copy;

	copy = cnp_strndup(orig, 7);
	if (copy == NULL) {
		return (TEST_FAIL);
	}

	if (strcmp(copy, "af56odi") != 0) {
		return (TEST_FAIL);
	}

	return (TEST_OK);
}

int
test_strnrchr(void)
{
	int ret;
	char orig[] = "af56,od|ais:auf#$s,iadjg";
	char *sp;

	if ((sp = cnp_strnrchr(orig, 10, 'a')) == NULL) {
		return (TEST_FAIL);
	}
	else if ((sp - orig) != 8) {
		return (TEST_FAIL);
	}

	if ((sp = cnp_strnrchr(orig, 20, '*')) != NULL) {
		return (TEST_FAIL);
	}

	return (TEST_OK);
}



int
main(int argc, char **argv)
{
	int result;
	unsigned int i;

	test_init();

	test_start("test_strdup()");
	result = test_strdup();
	test_end(result);

	test_start("test_strndup()");
	result = test_strndup();
	test_end(result);

	test_start("test_strnrchr()");
	result = test_strnrchr();
	test_end(result);

	test_cleanup();

	return (0);
}
